package com.heima.wemedia.config;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/***
 * 实现ApplicationRunner接口：
 *  程序启动完成某些初始化操作,比如初始化加载敏感词
 *
 *  读取数据库敏感词列表-一次操作
 *  加载到DFA算法中
 *  从DFA算法中检测是否包含敏感词
 */
@Component
public class RunnerStarter implements ApplicationRunner {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /***
     * 加载敏感词
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 查询所有敏感词
        List<WmSensitive> sensitiveList = wmSensitiveMapper.selectList(null);
        // 敏感词加载到DFA
        ArrayList<String> list = new ArrayList<>();
        for (WmSensitive wmSensitive : sensitiveList) {
            String sensitives = wmSensitive.getSensitives();
            list.add(sensitives);
        }
        SensitiveWordUtil.initMap(list);
    }
}